public class Convert {

    public static void main(String[] args) {
        Convert convert = new Convert();
        System.out.println(convert.convert("PAYPALISHIRING", 3));
    }

    public String convert(String s, int numRows) {
        if (numRows == 1) return s;
        char[] chars = s.toCharArray();
        int n = s.length();
        StringBuffer[] temp = new StringBuffer[numRows];
        int index = 0;
        for (int i = 0; i < numRows; i++) {
            temp[i] = new StringBuffer();
        }
        while (index < n) {
            for (int i = 0; i < numRows && index < n; i++) {
                temp[i].append(chars[index++]);
            }
            for (int i = numRows - 2; i > 0 && index < n; i--) {
                temp[i].append(chars[index++]);
            }
        }
        StringBuffer ans = new StringBuffer();
        for (int i = 0; i < numRows; i++) {
            ans.append(temp[i]);
        }
        return ans.toString();
    }
}
